//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Replication code for "Institutional Constraints on the Executive, Investment, and Elections"
//
// The code replicates the analyses discussed in footnote 15 of the article. In particular, it produces 
// comparisons of within-country change in PolConV, as well as changes in the number of 
// effective/independent institutions between the sample of OECD countries and the sample of countries
// used in the main analysis.
//////////////////////////////////////////////////////////////////////////////////////////////////////////

clear
global path="" //directory where datasets for analysis should be located
cd $path //set main folder directory as working directory

//if it does not already exist, create a "working data" folder in working directory
capture confirm file "./working data/"
if _rc mkdir "./working data/"

capture log close
capture log using "./working data/footnote15_analysis.log", replace
set more off


clear
use "main_analyses_and_supplemental_tables.dta" 

keep if main_sample==1
keep countryname year  president_fixed
save "./working data/henisz_analysis_country_years.dta", replace


clear

use "POLCON_2017.dta"
gen countryname =polity_country
replace countryname="Dominican Republic" if countryname=="Dominican Rep"
replace countryname="Congo, Rep." if countryname=="Congo, Brazzaville"
replace countryname="Cote d'Ivoire" if countryname=="Ivory Coast"
replace countryname="Gambia, The" if countryname=="Gambia"
replace countryname="Kyrgyz Republic" if countryname=="Kyrgyzstan"
replace countryname="North Macedonia" if countryname=="Macedonia"
replace countryname="South Korea" if countryname=="Korea, South"

drop if countryname==""
drop if year<1975 |year>2016

save "./working data/polcon_2017_wd.dta", replace

gen oecd=0
replace oecd =1 if countryname=="United States" |countryname=="Austria" |countryname=="Belgium" |countryname=="Canada" 
replace oecd =1 if countryname=="Netherlands" |countryname=="Greece" |countryname=="Sweden" |countryname=="Luxembourg" 
replace oecd =1 if countryname=="Switzerland" |countryname=="Norway" |countryname=="Turkey" |countryname=="Iceland" 
replace oecd =1 if countryname=="Denmark" |countryname=="Ireland" |countryname=="Portugal" |countryname=="United Kingdom" 
replace oecd =1 if countryname=="France" |countryname=="Italy" |countryname=="Spain" |countryname=="Germany" 
replace oecd =1 if countryname=="Chile" &year> 2010
replace oecd =1 if countryname=="Czech Republic" &year> 2010
replace oecd =1 if countryname=="Finland"
replace oecd =1 if countryname=="Israel" &year> 2010
replace oecd =1 if countryname=="South Korea" &year> 1996
replace oecd =1 if countryname=="Mexico" &year> 1994
replace oecd =1 if countryname=="New Zealand" 
replace oecd =1 if countryname=="Poland" &year> 1996
replace oecd =1 if countryname=="Slovakia" &year> 2000
replace oecd =1 if countryname=="Slovenia" &year> 2010
keep if oecd==1

bysort countryname: egen l1_sd = sd(l1)
bysort countryname: egen l2_sd = sd(l2)
bysort countryname: egen j_sd = sd(j)
bysort countryname: egen f_sd = sd(f)
bysort countryname: egen polconv_sd = sd(polconv)

gen any_institutional_change=0
replace any_institutional_change=1 if l1_sd>0 | l2_sd>0 | j_sd>0 | f_sd>0

gen any_polconv_change=0
replace any_polconv_change=1 if polconv_sd>0

bysort countryname: egen any_polconv_change_country = max(any_polconv_change)


by countryname, sort: gen nvals = _n == 1 
count if nvals==1 & any_polconv_change==1 & any_institutional_change==0 //countries without institutional change: 23
count if nvals==1 & any_polconv_change==1 & any_institutional_change==1 //countries with institutional change 7
collapse (sd) polconv  , by(countryname)
sum polconv //mean within-country standard deviation of PolConV for OECD countries (country-year observations, 1975-2016 when countries in OECD): 0.04

clear

use "./working data/polcon_2017_wd.dta"
merge 1:1 countryname year using "./working data/henisz_analysis_country_years.dta"

tab _merge
keep if _merge==3
sort countryname year

bysort countryname: egen l1_sd = sd(l1)
bysort countryname: egen l2_sd = sd(l2)
bysort countryname: egen j_sd = sd(j)
bysort countryname: egen f_sd = sd(f)
bysort countryname: egen polconv_sd = sd(polconv)

gen any_institutional_change=0
replace any_institutional_change=1 if l1_sd>0 | l2_sd>0 | j_sd>0 | f_sd>0

gen any_polconv_change=0
replace any_polconv_change=1 if polconv_sd>0

bysort countryname: egen any_polconv_change_country = max(any_polconv_change)


by countryname, sort: gen nvals = _n == 1 


count if any_polconv_change==1 & any_institutional_change==0 //number of observations accounted for by countries without instutional change: 342
count if any_polconv_change==1 & any_institutional_change==1 //number of observations accounted for by countries without instutional change: 718

collapse (sd) polconv  , by(countryname)
sum polconv //mean within-country standard deviation of PolConV for countries in main sample: 0.13
////////////////////


log close

